// 将base64字符串转换为Blob对象
function base64ToBlob(base64) {
  let parts = base64.split(';base64,');
  let contentType = parts[0].split(':')[1];
  let raw = window.atob(parts[1]);
  let rawLength = raw.length;
  let uInt8Array = new Uint8Array(rawLength);
  for (let i = 0; i < rawLength; ++i) {
    uInt8Array[i] = raw.charCodeAt(i);
  }
  return new Blob([uInt8Array], { type: contentType });
}

// 创建一个URL指向Blob对象
function blobToUrl(blob) {
  return URL.createObjectURL(blob);
}

function isBase64(str) {
  return str.indexOf(';base64,') !== -1;
}

function isUrl(str) {
  let pattern = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i;
  return pattern.test(str);
}

export default function (base64String) {
  if (isUrl(base64String)) return base64String;
  if (!isBase64(base64String)) return null;

  const blob = base64ToBlob(base64String);
  const url = blobToUrl(blob);
  return url || null;
}
